package com.whut.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.whut.mapper.ManageinfoMapper;
import com.whut.mapper.OrderinfoMapper;
import com.whut.mapper.UserinfoMapper;
import com.whut.model.Userinfo;
import com.whut.model.Orderinfo;
import com.whut.model.Manageinfo;
import com.whut.service.UserService;
import com.whut.websocket.WebSocketServer;
import org.springframework.stereotype.Service;
import com.whut.util.MD5Encoder;

import javax.annotation.Resource;
import java.util.List;

@Service("useService")
public class UserServiceImpl implements UserService {

    private static final String ERROR = "error";
    private static final String SUCCESS = "success";
    @Resource
    private UserinfoMapper userinfoMapper;
    @Resource
    private OrderinfoMapper orderinfoMapper;
    @Resource
    private ManageinfoMapper manageinfoMapper;

    @Override
    public String insertOne(Userinfo userinfo) {
        try {
            userinfoMapper.insertSelective(userinfo);
        }catch (Exception e){
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }

    @Override
    public Userinfo getUserById(int mKey) {
        return userinfoMapper.selectByPrimaryKey(mKey);
    }

    @Override
    public Object checkPassword(String tel, String password) {
        String realPwd = MD5Encoder.getMD5(password);
        Userinfo userinfo = userinfoMapper.selectByUserPhone(tel);
        JSONObject jsonObject = new JSONObject();
        if (userinfo != null && userinfo.getPassword().equals(realPwd)) {
            jsonObject.put("isRight", true);
            JSONObject userJSON = (JSONObject) JSONObject.toJSON(userinfo);
            jsonObject.put("data", userJSON);
        } else {
            jsonObject.put("isRight", false);
        }
        return jsonObject;
    }



    @Override
    public Userinfo getStaffInfoBytel(String tel) {
        return userinfoMapper.selectByUserPhone(tel);
    }

    @Override
    public String addUser(Userinfo userinfo) {
        try {
            userinfoMapper.insert(userinfo);
            System.out.println(userinfo);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }
    /***wxd***/
    @Override
    public String leave(Manageinfo manageinfo) {

        manageinfoMapper.leave(manageinfo);
        //通知所有管理员请假信息
        List<Userinfo> list = userinfoMapper.getAllAdmin();
        for (Userinfo user:list) {
            WebSocketServer ws = WebSocketServer.getInstance(user.getId());
            if(ws!=null){ws.sendMessage("somebodyAskLeaving");}
        }
        return "success";
    }
    /***wxd***/
    @Override
    public String sign(Manageinfo manageinfo) {
        manageinfoMapper.sign(manageinfo);
        return "success";
    }
    /***wxd***/
    @Override
    public List<Object> manage(Integer id) {
        return manageinfoMapper.manage(id);
    }
    /***wxd***/
    @Override
    public String getJobById(Integer id){ return  userinfoMapper.getJobById(id);}
    /***wxd***/
    @Override
    public List<Object> room() {
        return manageinfoMapper.room();
    }


    @Override
    public List<Object> getOrderInfo() {
        return orderinfoMapper.selectOrderInfo();
    }

    /*wyh*/
    @Override
    public Object getAllUserInfo() {
        List<Userinfo> userList = userinfoMapper.getAllUserInfo();
        System.out.println(userList);
        return userList;
    }


    /**
     * @param (id,serve,job)
     * @return java.lang.Object
     * @auther wenyihan
     * 分别在userInfo和manageInfo表中查找的职员对其进行任职修改及任职时间进行修改，返回的是修改过的userinfo表
     * @date 10:21 2019/10/30
     */
    @Override
    public Object editServeInfoById(int id, String serve, String job) {
        int userinfoServe = userinfoMapper.editUserServeInfo(id, serve);
        int manageinfoServe = manageinfoMapper.editManageServerInfo(id, serve);
        int userinfoServe1 = userinfoMapper.editUserJobInfo(id, job);
        List<Userinfo> userinfoList = userinfoMapper.getAllUserInfo();
        return userinfoList;
    }

    /*wyh*/
    @Override
    public Object getAllAdmin() {
        List<Userinfo> userinfoList = userinfoMapper.getAllAdmin();
        return userinfoList;
    }
    /**
     * @param （nickName，sex）
     * @return
     * @auther lyl
     * //TODO
     * @date 11:22 2019/11/5
     */
    @Override
    public Object addUserInfo(Integer id, String nickName, String sex) {
        Userinfo p = new Userinfo();
        p.setId(id);
        p.setSex(sex);
        p.setNickName(nickName);
        userinfoMapper.updateByPrimaryKeySelective(p);
        return null;
    }

    @Override
    public Object getUserInfo(Integer id) {
        return userinfoMapper.getSingleUserInfoById(id);
    }
}
